/*
 * parserfunction.c
 *
 *  Created on: 2013-5-15
 *      Author: xkaying
 */

#include "parserfunction.h"
#include "../typedef/function.h"

#include <assert.h>

#include "parser.h"

Function *ParserFunction(Lexer *lexer, Function *pfun)
{
	assert(lexer);
	if (pfun == 0) {
		//fun为0 的话表示是个接口 public non-final non-inline
		pfun = FunctionInit(2, 0, 0);
	}
	int state;
	state = 0;
	ptoken = LexerGetToken(lexer);
	while (ptoken) {
		switch (state)
		{
			case 0: {
				switch (ptoken->type)
				{
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Variable: {
						state = 1;
						FunctionAppendName(pfun, ptoken->name);
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:错误的函数定义.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser function状态机错误.状态%d.\n", state);
				return 0;
			}
			case 1: {
				switch (ptoken->type)
				{
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_BracketStart: {
						state = 2;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:错误的函数定义.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser function状态机错误.状态%d.\n", state);
				return 0;
			}
			case 2: {
				switch (ptoken->type)
				{
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_BracketEnd: {
						state = 3;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Variable: {
						//input list 读取
					}
					default: {
						printf("第%d行:错误的函数定义.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser function状态机错误.状态%d.\n", state);
				return 0;
			}
			case 3: {
				switch (ptoken->type)
				{
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_End:{
						//表示定义结束
						ptoken=LexerGetToken(lexer);
						return pfun;
					}
					//函数实际内容读取
					case TokenType_BlockStart: {
						state = 4;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Extend: {
						state = 30;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:错误的函数定义.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser function状态机错误.状态%d.\n", state);
				return 0;
			}
		}
	}
	return 0;
}
